package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.exception.LyException;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.mapper.SpecParamMapper;
import com.leyou.item.service.SpecParamService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author YYP
 * @date 2020/12/29
 */
@Service
public class SpecParamServiceImpl extends ServiceImpl<SpecParamMapper, SpecParam> implements SpecParamService {

    /**
     * 修改规格参数
     * @param specParamDTO
     */
    @Override
    public void updateSpecParam(SpecParamDTO specParamDTO) {
        SpecParam specParam = specParamDTO.toEntity(SpecParam.class);
        this.updateById(specParam);
    }

    /**
     * 新增规格参数
     * @param specParamDTO
     */
    @Override
    public void addSpecParam(SpecParamDTO specParamDTO) {
        SpecParam specParam = specParamDTO.toEntity(SpecParam.class);
        this.save(specParam);
    }

    /**
     * 查询规格参数集合
     * @param categoryId 分类id
     * @param groupId 规格组id
     * @param searching 是否搜索
     * @return 参数集合
     */
    @Override
    public List<SpecParamDTO> listParamByQuery(Long categoryId, Long groupId, Boolean searching) {

        //健壮性判断
        if (categoryId == null && groupId == null) {
            throw new LyException(400,"查询条件不能为空");
        }
        //查询
        List<SpecParam> list = this.query()
                .eq(categoryId != null, "category_id", categoryId)
                .eq(groupId != null, "group_id", groupId)
                .eq(searching != null, "searching", searching)
                .list();
        List<SpecParamDTO> specParamDTOS = SpecParamDTO.convertEntityList(list);
        return specParamDTOS;
    }
}
